MineView = {}
local self = MineView

self.view = UIBase()
self.view.name = "MineView"
self.view:AddPath("ui/panels/mine.gdmec")
self.view.onInit = function()
    self:OnInit()
end

self.view.onUnit = function()
    self:OnUnit()
end

function self:Close()
    self.view:Close()
end

function self:Open()
    self.view:Open()
end

function self:OnInit()
    self.childs = {}
    self.ui_btn_close = self.view.go.transform:Find("BtnClose"):GetComponent(typeof(UnityEngine.UI.Button))
    self.ui_btn_close.onClick:AddListener(
        function()
            self:Close()
        end
    )
    self.ui_btn_diff = self.view.go.transform:Find("Diff/BtnDiff"):GetComponent(typeof(UnityEngine.UI.Button))
    self.ui_btn_diff.onClick:AddListener(
        function()
            MineControl:SetDiff(MineConstant.DiffLev.I)
        end
    )

    self.ui_btn_go = self.view.go.transform:Find("BtnGo"):GetComponent(typeof(UnityEngine.UI.Button))
    self.ui_btn_go.onClick:AddListener(
        function()
            MineControl:MineStart()
        end
    )

    self.ui_mine_root = self.view.go.transform:Find("MineRoot")
    self.ui_mine_prefab = self.view.go.transform:Find("MineRoot/MinePrefab")
    self.ui_mine_prefab.gameObject:SetActive(false)

    self.func_diff = function()
        self:UpView()
    end
    self.func_mine_start = function()
        self:UpView()
    end
    EventListener:Listener(MineEvent.MINE_DIFF_UP, self.func_diff)
    EventListener:Listener(MineEvent.MINE_UP, self.func_mine_start)
end

function self:UpView()
    for k, v in pairs(self.childs) do
        v.go.gameObject:SetActive(false)
    end
    local diff_args = MineConstant.DiffArgs[MineModel.diff_lev]
    local idx = 0
    local child = nil
    local grid = nil
    for y = 1, diff_args.Y do
        for x = 1, diff_args.X do
            --刷新状态
            grid = MineModel:GetGrid(x, y)
            if grid then
                idx = x + (y - 1) * diff_args.X
                child = self:MakeChild(idx, x, y)
                child.txt_idx.text = string.format("%s <color=#FF0000>x:%s y:%s </color>", idx, x, y)
                if MineModel.game_status == MineConstant.GameStatus.Start  then  
                    --[[
                         bomb_count = 0,
                         is_bomb = false,
                         is_dig = false    
                    --]]
                    if grid.is_dig then 
                       child.txt_num.gameObject:SetActive(grid.bomb_count ~= 0)
                       child.dig.gameObject:SetActive(true)
                       child.txt_num.text = grid.bomb_count
                    end
                elseif MineModel.game_status == MineConstant.GameStatus.GameOver  then 
                      child.txt_num.gameObject:SetActive(true)
                      child.dig.gameObject:SetActive(true)
                      child.bomb.gameObject:SetActive(grid.is_bomb)
                      child.txt_num.text = grid.bomb_count
                end
            end
        end
    end
end

function self:Dig(x, y)
    local grid = MineModel:GetGrid(x, y)
    if grid and grid.is_dig then
        return
    end
    MineControl:Dig(x, y)
end


function self:MakeChild(idx, x, y)
    local child = self.childs[idx]
    if child == nil then
        local go = UnityEngine.GameObject.Instantiate(self.ui_mine_prefab, self.ui_mine_root)
        child = {
            go = go,
            txt_num = go.transform:Find("Num"):GetComponent(typeof(UnityEngine.UI.Text)),
            txt_idx = go.transform:Find("Idx"):GetComponent(typeof(UnityEngine.UI.Text)),
            dig = go.transform:Find("Dig"),
            bomb = go.transform:Find("Bomb"),
            btn = go:GetComponent(typeof(UnityEngine.UI.Button)),
            x = x,
            y = y
        }
        child.btn.onClick:AddListener(
            function()
                self:Dig(x,y)
            end
        )
        self.childs[idx] = child
    end
    child.txt_num.gameObject:SetActive(false)
    child.dig.gameObject:SetActive(false)
    child.bomb.gameObject:SetActive(false)
    child.go.gameObject:SetActive(true)
    child.txt_num.text = ""
    return child
end

function self:OnUnit()
    EventListener:Remove(MineEvent.MINE_DIFF_UP, self.func_diff)
end
